// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2012 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;

namespace YourCompany.Modules.DynamicText
{
    using global::DynamicText.Components.DataProvider;

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// SQL Server implementation of the abstract DataProvider class 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public class SqlDataProvider : IDataProvider
    {


        #region "Private Members"

        private const string ModuleQualifier = "YourCompany_";

        private string _connectionString = string.Empty;
        private string _objectQualifier = string.Empty;
        private string _databaseOwner = string.Empty;

        #endregion

        #region "Constructors"

        #endregion

        #region "Properties"

        public string ConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(_connectionString))
                {
                    return DotNetNuke.Data.DataProvider.Instance().ConnectionString;
                }
                return _connectionString;
            }
            set { _connectionString = value; }
        }

        public string ObjectQualifier
        {
            get
            {
                if (_objectQualifier == String.Empty)
                {
                    return DotNetNuke.Data.DataProvider.Instance().ObjectQualifier;
                }
                return _objectQualifier;
            }
            set { _objectQualifier = value; }
        }

        public string DatabaseOwner
        {
            get
            {
                if (_databaseOwner == String.Empty)
                {
                    return DotNetNuke.Data.DataProvider.Instance().DatabaseOwner;
                }
                return _databaseOwner;
            }
            set { _databaseOwner = value; }
        }

        #endregion

        #region "Private Methods"

        private string GetFullyQualifiedName(string name)
        {
            return DatabaseOwner + ObjectQualifier + ModuleQualifier + name;
        }

        #endregion

        #region "Public Methods"

        public IDataReader GetDynamicTexts(int ModuleId)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GetDynamicTexts"), ModuleId);
        }

        public IDataReader GetDynamicText(int ModuleId, int ItemId)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GetDynamicText"), ModuleId, ItemId);
        }

        public int AddDynamicText(int ModuleId, string Content, int UserId)
        {
            return SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("AddDynamicText"), ModuleId, Content, UserId);
        }

        public void UpdateDynamicText(int ModuleId, int ItemId, string Content, int UserId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("UpdateDynamicText"), ModuleId, ItemId, Content, UserId);
        }

        public void DeleteDynamicText(int ModuleId, int ItemId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("DeleteDynamicText"), ModuleId, ItemId);
        }

        #endregion

    }
}